package com.suian.spring.test;

public abstract class LogHandler {
    protected LogHandler nextHandler;
    protected LogLevel level;

    public LogHandler(LogLevel level) {
        this.level = level;
    }

    //设置下一个处理器
    public LogHandler setNext(LogHandler nextHandler) {
        this.nextHandler = nextHandler;
        return nextHandler;
    }

    //处理日志请求
    public void handleRequest(LogRequest request) {
        if (this.level == request.getLevel()) {
            this.writeLog(request);
        } else if (nextHandler != null) {
            //传递给下一个处理器
            nextHandler.handleRequest(request);
        } else {
            //没有处理器能处理时的默认行为
            System.out.println("未处理的日志级别：" + request.getLevel());
        }
    }

    protected abstract void writeLog(LogRequest request);
}
